﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoPortal.Filters;
using TaoPortal.Areas.Admin.Models;
using TaoPortal.Common;
using TaoPortal.Common.Utility;


namespace TaoPortal.Areas.Admin.Controllers
{
    [Compress]
    //[SysAuthorize(Roles = "admin")]
    public class BaseController : Controller
    {
        /// <summary>
        /// 当前用户信息
        /// </summary>
        public SessionEnt SessionEnt
        {
            get
            {
                //return Session[PublicKey.IDENTITY_KEY] as SessionEnt;
                return new SessionEnt { UserName = "admin" };
            }
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            foreach (var item in filterContext.ActionParameters)
            {
                if (item.Value is BaseModel)
                {
                    var model = item.Value as BaseModel;
                    model.SessionEnt = this.SessionEnt;
                    model.Session = this.Session;
                }
            }
        }

        #region 添加缓存
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public static void AddCache(string key, object value)
        {
            HttpContextCache.Add(key, value);
        }
        #endregion

        #region 获取缓存
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">key</param>
        /// <returns></returns>
        public static object GetCache(string key)
        {
            return HttpContextCache.Get(key);
        }
        #endregion

        #region 获取视图HTML
        /// <summary>
        /// 获取视图HTML
        /// </summary>
        /// <param name="viewName">视图名称</param>
        /// <param name="masterName">母版页</param>
        /// <returns></returns>
        public string GetViewHtml(string viewName, string masterName)
        {
            IView view = ViewEngines.Engines.FindView(ControllerContext, viewName, masterName).View;
            using (StringWriter writer = new StringWriter())
            {
                ViewContext viewContext = new ViewContext(ControllerContext, view, ViewData, TempData, writer);
                viewContext.View.Render(viewContext, writer);
                return writer.ToString();
            }
        }
        #endregion

        #region 获取部分视图HTML
        /// <summary>
        /// 获取部分视图HTML
        /// </summary>
        /// <param name="viewName">视图名称</param>
        /// <returns></returns>
        public string GetPartViewHtml(string viewName)
        {
            IView view = ViewEngines.Engines.FindPartialView(ControllerContext, viewName).View;
            using (StringWriter writer = new StringWriter())
            {
                ViewContext viewContext = new ViewContext(ControllerContext, view, ViewData, TempData, writer);
                viewContext.View.Render(viewContext, writer);
                return writer.ToString();
            }
        }
        #endregion

        #region 将对象序列化成Json字符串
        /// <summary>
        /// 将对象序列化成Json字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string SerializeJson(object obj)
        {
            var str = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(obj);
            return str;
        }
        #endregion

    }
}
